﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IHISCETeim.DAL;
using IHISCETeim.Model;
using IHISCETeim.Library;
using System.Data;
using IHISCETeim.Web;

namespace IHISCETeim.BLL
{
    public class AgentQltyController
    {
        /// <summary>
        /// 获取配件需求情况分类，与mttr时间计算有关系
        /// 
        /// </summary>
        /// <param name="listParts"></param>
        /// <returns>无配件需求,索赔结算,索赔发货,供应商发货</returns>
        public static string GetPartsRequestType(List<AgentQltyPartsInfo> listParts)
        {
            int allCount = listParts.Count;
            if (listParts.Count() <= 0)
                return "无配件需求";

            if (listParts.Count(e => e.PartsDemand == "索赔结算") == allCount)
            {
                return "索赔结算";
            }
            if (listParts.Count(e => e.PartsDemand == "备用件发货") == allCount)
            {
                return "备用件发货";
            }
            if ((listParts.Count(e => e.PartsDemand == "备用件发货") + listParts.Count(e => e.PartsDemand == "索赔结算")) == allCount)
            {
                return "索赔结算";
            }


            if (listParts.Count(e => e.PartsDemand == "供应商发货") > 0)
            {
                return "供应商发货";
            }

            if (listParts.Count(e => e.PartsDemand == "索赔加急发货") > 0 || listParts.Count(e => e.PartsDemand == "索赔发货(补件)") > 0 || listParts.Count(e => e.PartsDemand == "索赔发货") > 0 || listParts.Count(e => e.PartsDemand == "服务支持件") > 0)
            {
                return "索赔发货";
            }

            return "";

        }
        public static int Add(AgentQltyInfo obj, List<AgentQltyImgInfo> listImg, List<AgentQltyPartsInfo> listParts)
        {
            return Add(obj, listImg, listParts, true);
        }
        public static int Add(AgentQltyInfo obj, List<AgentQltyImgInfo> listImg, List<AgentQltyPartsInfo> listParts, bool isneedSendEmail)
        {
            int returnval = AgentQltyDAL.Add(obj, listImg, listParts);
            if (returnval > 0 && isneedSendEmail)
            {
                obj = GetByQltyId(returnval);
                SEMailListDAL.Add(AgentEmailController.GetAddQltyEmail(obj));
            }
            return returnval;
        }

        /// <summary>
        /// 根据品情号获取其附属的品情号
        /// </summary>
        /// <param name="ClaimsNumber"></param>
        /// <returns></returns>
        public static string GetNewClaimsNumber(string ClaimsNumber)
        {
            if (ClaimsNumber.IndexOf("-") > 0)
            {
                ClaimsNumber = ClaimsNumber.Substring(0, ClaimsNumber.IndexOf("-"));
            }
            return AgentQltyDAL.GetNewClaimsNumber(ClaimsNumber.ToUpper());
        }
        /// <summary>
        /// 取消的品情重新提交
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="listImg"></param>
        /// <param name="listParts"></param>
        /// <returns></returns>
        public static bool UpdateCancelQlty(AgentQltyInfo obj, List<AgentQltyImgInfo> listImg, List<AgentQltyPartsInfo> listParts)
        {
            bool returnval = AgentQltyDAL.UpdateCancelQlty(obj, listImg, listParts, AgentEmailController.GetAddQltyEmail(obj, true));
            //if (returnval )
            //{
            //    AgentEmailController.AddQltyEmail(obj.QltyId,true);
            //}
            return returnval;
        }

        /// <summary>
        /// 确认完品情后，添加相应的索赔信息
        /// </summary>
        /// <returns></returns>
        private static bool AddUClaimDemageControl(AgentQltyInfo obj)
        {
            try
            {
                UClaimDemageControlInfo UCDObj = new UClaimDemageControlInfo();
                UCDObj.ClaimsNumber = obj.ClaimsNumber;
                UCDObj.SerialNumber = obj.SerialNumber;
                UCDObj.FaultDate = obj.FaultDate;
                UCDObj.FaultDescription = obj.FaultDescription;
                UCDObj.AgentName = obj.AgentName;
                UCDObj.CustomerName = obj.CustomerName;
                UCDObj.MachineType = obj.MachineType;
                UCDObj.WorkHour = obj.WorkHour;
                UCDObj.ProcessMode = obj.SvrResult + obj.SvrScheme;

                return UClaimDemageControlController.Add(UCDObj) > 0;
            }
            catch (Exception exc)
            {
                SEMailListController.AddAppError(OUserController.CurrentUser.RealName, "AgentQltyController.AddUClaimDemageControl()添加相应的索赔信息时出错,请手工添加,索赔号为:" + obj.ClaimsNumber, exc.Message);
                return false;
            }
        }
        /// <summary>
        /// 服务担当修改
        /// </summary>
        /// <param name="pass">0为表通过,1表示品情驳回,2表示品情拒培</param>
        /// <param name="obj"></param>
        /// <param name="listImg"></param>
        /// <param name="listParts"></param>
        /// <returns>
        /// 0.表示处理失败
        /// 1.表示服务经理审核中
        /// 2.表示品情已确认 ,并需要发货,已通知配件部发货
        /// 3.表示品情已确认 ,但不需要发货
        /// 4.表示品情驳回,并通知经销商
        /// 5.表示品情拒赔,并通知经销商
        /// </returns>
        public static int SvrActEdit(int passtype, AgentQltyRejectInfo rejectObj, int SvrUserId, AgentQltyInfo obj, List<AgentQltyImgInfo> listImg, List<AgentQltyPartsInfo> listParts)
        {

            int returnVal = 0;
            //处理是否需要服务经理审核

            //todo:2014-08-26 处理新的流程
            AgentPowerConfigInfo power = AgentConfigController.GetByUserId(obj.SrvActUserId);
            AgentPowerConfigInfo SvrManagerPower = AgentConfigController.GetById(power.ParId);
            decimal ClaimAmount = power.ClaimAmount;
            decimal nowPartsAmount = listParts.Sum(e => e.LastPurPrice * e.Quantity);
            int zeroPartNum = listParts.Count(e => e.LastPurPrice <= 0);//配件最后采购件为0的个数
            //品情驳回
            if (passtype == 1)
            {
                //obj.SrvMangerUserId = 0;
                obj.ClaimState = ClaimState.品情再确认.ToString();
            }
            if (passtype == 2)
            {
                //obj.SrvMangerUserId = 0;
                obj.ClaimState = ClaimState.品情拒赔.ToString();
            }
            if (passtype == 0)
            {
                //担当回复日,只能记录第一次,服务经理回复日默认与担当一致
                obj.SrvActReplyDate = obj.SrvActReplyDate == DateTime.MinValue ? System.DateTime.Now : obj.SrvActReplyDate;
                obj.SrvMangerReplyDate = obj.SrvActReplyDate;

                obj.PartsRequestType = GetPartsRequestType(listParts);

                if (zeroPartNum > 0 && (obj.PartsRequestType == "索赔发货" || obj.PartsRequestType == "供应商发货"))
                {
                    //如果配件金额小于0,则需要由部长直接审核
                    AgentPowerConfigInfo topMgr = AgentConfigController.GetTopSvrMgr(obj.SrvActUserId);
                    obj.SrvMangerUserId = topMgr.UserId;
                    obj.ClaimState = topMgr.ShowClaimState; ;
                }
                else
                {
                    if (nowPartsAmount > ClaimAmount)//配件金额权限判断
                    {

                        //需发邮件给服务经理
                        obj.SrvMangerUserId = SvrManagerPower.UserId;
                        obj.ClaimState = SvrManagerPower.ShowClaimState;// ClaimState.服务经理审核中.ToString();
                    }
                    else
                    {
                        //需发邮件给配件部                    

                        if (obj.PartsRequestType == "索赔发货" || obj.PartsRequestType == "供应商发货")
                            obj.NoticeSendPartsDate = DateTime.Now;//通知配件发货时间

                        obj.NoticeSendPartsDate = DateTime.Now;//品情确认时间
                        obj.ClaimState = ClaimState.品情已确认.ToString();
                        // obj.IssueDate = DateTime.Now;//2014-10-27 将发行日改成品情确认日
                    }
                }
            }

            //2014-10-12 调整处理过程,保证所有操作都能正常完成
            #region 发邮件处理
            //发邮件处理
            List<SEMailListInfo> objEmails = new List<SEMailListInfo>();
            //如果是提交品情确认,并需要提交内部通报给经理
            if (passtype == 0 && obj.IsNeedAppove)
            {
                objEmails.Add(AgentEmailController.GetSvrMgrSvrSchemEmail(obj));
            }
            //
            if (obj.ClaimState == ClaimState.服务经理审核中.ToString() || obj.ClaimState == ClaimState.服务部长审核中.ToString() || obj.ClaimState == ClaimState.技术经理审核中.ToString())
            {
                //20150702 加入配件金额小于等于0时，通知服务经理
                SEMailListInfo emailObj = AgentEmailController.GetSvrMgrEmail(obj, listParts);
                emailObj.MailTo += ";huangkaiji@ihisce.com";
                objEmails.Add(emailObj);
                returnVal = 1;
            }
            if (obj.ClaimState == ClaimState.品情已确认.ToString())
            {
                if (listParts.Count(e => e.PartsDemand == "索赔加急发货") > 0 || listParts.Count(e => e.PartsDemand == "索赔发货") > 0 || listParts.Count(e => e.PartsDemand == "服务支持件") > 0 || listParts.Count(e => e.PartsDemand == "备用件发货") > 0 || listParts.Count(e => e.PartsDemand == "索赔发货(补件)") > 0)//如果有需要索赔发货的才通知配件部
                {
                    objEmails.Add(AgentEmailController.GetSendPartsEmail(obj, listParts));
                    returnVal = 2;//发邮件
                }
                if (listParts.Count(e => e.PartsDemand == "供应商发货") > 0)//如果有需要供应商发货的才通知服务内群
                {
                    objEmails.Add(AgentEmailController.GetSendPartsToIHISCESvrSupperEmail(obj, listParts));
                    returnVal = 2;//发邮件
                }
                objEmails.Add(AgentEmailController.GetEndEmail(obj));
                returnVal = returnVal > 0 ? returnVal : 3;
                AddUClaimDemageControl(obj);

                //TODO:需要邮件通知抄送人员20150914
                string emails = "";
                StringBuilder sb = new StringBuilder();
                string[] ccemails = obj.CCPartEmail.Split(new char[] { ',' });
                foreach (string item in ccemails)
                {
                    string _e = AgentConfigController.GetValue(item);
                    if (string.IsNullOrEmpty(_e))
                        continue;
                    emails += _e + ";";
                    sb.Append(item);
                    sb.Append(",");
                }
                sb.ToString().TrimEnd(',');
                if (!string.IsNullOrEmpty(emails))
                {
                    SEMailListInfo objemail = AgentEmailController.GetApproveSchemeEmail(obj.QltyId, emails, obj.SvrScheme, "1");
                    objEmails.Add(objemail);
                }
            }
            if (obj.ClaimState == ClaimState.品情再确认.ToString())
            {
                objEmails.Add(AgentEmailController.GetCancelQltyEmail(obj));
                returnVal = 4;
            }
            if (obj.ClaimState == ClaimState.品情拒赔.ToString())
            {
                objEmails.Add(AgentEmailController.GetExclusionsQltyEmail(obj));
                returnVal = 5;
            }
            #endregion


            bool result = AgentQltyDAL.SvrActEdit(rejectObj, obj, listImg, listParts, objEmails);

            return result ? returnVal : 0;
            /*
            if (result)//数据库处理完成
            { 
                //发邮件处理
                if (obj.ClaimState == ClaimState.服务经理审核中.ToString() || obj.ClaimState == ClaimState.服务部长审核中.ToString())
                {
                    AgentEmailController.SvrMgrEmail(obj.QltyId);
                    returnVal = 1;
                }
                if (obj.ClaimState == ClaimState.品情已确认.ToString())
                {
                    if (listParts.Count(e => e.PartsDemand == "索赔发货") > 0)//如果有需要索赔发货的才通知配件部
                    {
                        AgentEmailController.SendPartsEmail(obj.QltyId);
                        returnVal = 2;//发邮件
                    }
                    if (listParts.Count(e => e.PartsDemand == "供应商发货") > 0)//如果有需要供应商发货的才通知服务内群
                    {
                        AgentEmailController.SendPartsToIHISCESvrSupperEmail(obj.QltyId);
                        returnVal = 2;//发邮件
                    }
                    AgentEmailController.EndEmail(obj.QltyId);
                    returnVal=returnVal>0?returnVal: 3;
                    AddUClaimDemageControl(obj);
                }
                if (obj.ClaimState == ClaimState.品情驳回.ToString())
                {
                    AgentEmailController.CancelQltyEmail(obj.QltyId);
                    returnVal = 4;
                }
                if (obj.ClaimState == ClaimState.品情拒赔.ToString())
                {
                    AgentEmailController.ExclusionsQltyEmail(obj.QltyId);
                    returnVal =5;
                }
            }*/


        }

        /// <summary>
        /// 服务经理 审核
        /// </summary>
        /// <param name="pass"></param>
        /// <param name="obj"></param>
        /// <param name="listImg"></param>
        /// <param name="listParts"></param>
        /// <returns></returns>
        public static int SvrMgrEdit(bool pass, AgentQltyRejectInfo rejectObj, AgentQltyInfo obj, List<AgentQltyImgInfo> listImg, List<AgentQltyPartsInfo> listParts)
        {
            //TODO:By yyx 服务担当处理后,需要邮件处理代码
            int returnVal = 0;
            decimal nowPartsAmount = listParts.Sum(e => e.LastPurPrice * e.Quantity);
            int zeroPartNum = listParts.Count(e => e.LastPurPrice <= 0);//配件最后采购件为0的个数
            if (pass)
            {
                //todo:2014-08-26 处理新的流程
                AgentPowerConfigInfo power = AgentConfigController.GetByUserId(obj.SrvMangerUserId);
                AgentPowerConfigInfo SvrManagerPower = AgentConfigController.GetById(power.ParId);
                decimal ClaimAmount = power.ClaimAmount;

                if (nowPartsAmount > ClaimAmount && power.ParId > 0)//配件金额权限判断
                {
                    //需发邮件给上级
                    obj.SrvMangerUserId = SvrManagerPower.UserId;
                    obj.ClaimState = SvrManagerPower.ShowClaimState;// ClaimState.服务经理审核中.ToString();
                }
                else//不超过权限或是最上级
                {
                    obj.SrvMangerReplyDate = System.DateTime.Now;

                    obj.PartsRequestType = GetPartsRequestType(listParts);
                    if (obj.PartsRequestType == "索赔发货" || obj.PartsRequestType == "供应商发货")
                        obj.NoticeSendPartsDate = DateTime.Now;//通知配件发货时间

                    obj.NoticeSendPartsDate = DateTime.Now;//品情确认时间
                    obj.ClaimState = ClaimState.品情已确认.ToString();
                    //obj.IssueDate = DateTime.Now;//2014-10-27 将发行日改成品情确认日
                }
            }
            else
            {
                obj.ClaimState = ClaimState.品情再确认.ToString();
            }

            //2014-10-12 调整处理过程,保证所有操作都能正常完成
            #region 发邮件处理
            List<SEMailListInfo> objEmails = new List<SEMailListInfo>();
            if (obj.ClaimState == ClaimState.服务部长审核中.ToString() || obj.ClaimState == ClaimState.服务经理审核中.ToString())
            {
                objEmails.Add(AgentEmailController.GetSvrMgrEmail(obj, listParts));
                returnVal = 1;
            }

            if (obj.ClaimState == ClaimState.品情已确认.ToString())
            {
                if (listParts.Count(e => e.PartsDemand == "索赔加急发货") > 0 || listParts.Count(e => e.PartsDemand == "索赔发货") > 0 || listParts.Count(e => e.PartsDemand == "服务支持件") > 0 || listParts.Count(e => e.PartsDemand == "备用件发货") > 0 || listParts.Count(e => e.PartsDemand == "索赔发货(补件)") > 0)//如果有需要索赔发货的才通知配件部
                {
                    objEmails.Add(AgentEmailController.GetSendPartsEmail(obj, listParts));
                    returnVal = 2;//发邮件
                }
                if (listParts.Count(e => e.PartsDemand == "供应商发货") > 0)//如果有需要供应商发货的才通知服务内群
                {
                    objEmails.Add(AgentEmailController.GetSendPartsToIHISCESvrSupperEmail(obj, listParts));
                    returnVal = 2;//发邮件
                }
                objEmails.Add(AgentEmailController.GetEndEmail(obj));
                //2015-06-12 品情确认后，如果金额超出限制，则发邮件通知应总
                if (nowPartsAmount >= AgentConfigController.QltyAlarmMoney)
                {
                    objEmails.Add(AgentEmailController.GetAlarmMoneySendEmail(obj, listParts));
                }
                returnVal = returnVal > 0 ? returnVal : 3;
                AddUClaimDemageControl(obj);


                //TODO:需要邮件通知抄送人员20150914
                string emails = "";
                StringBuilder sb = new StringBuilder();
                string[] ccemails = obj.CCPartEmail.Split(new char[] { ',' });
                foreach (string item in ccemails)
                {
                    string _e = AgentConfigController.GetValue(item);
                    if (string.IsNullOrEmpty(_e))
                        continue;
                    emails += _e + ";";
                    sb.Append(item);
                    sb.Append(",");
                }
                sb.ToString().TrimEnd(',');
                if (!string.IsNullOrEmpty(emails))
                {
                    SEMailListInfo objemail = AgentEmailController.GetApproveSchemeEmail(obj.QltyId, emails, obj.SvrScheme, "2");
                    objEmails.Add(objemail);
                }

            }

            if (obj.ClaimState == ClaimState.品情再确认.ToString())
            {
                objEmails.Add(AgentEmailController.GetNoPassEmail(obj, OUserController.CurrentUser.UserId));
                returnVal = 5;
            }
            #endregion

            bool result = AgentQltyDAL.SvrActEdit(rejectObj, obj, listImg, listParts, objEmails);
            return result ? returnVal : 0;
            /*
            if (result)
            {
                if (obj.ClaimState == ClaimState.服务部长审核中.ToString())
                {
                    AgentEmailController.SvrMgrEmail(obj.QltyId);
                    returnVal = 1;
                }

                if (obj.ClaimState == ClaimState.品情已确认.ToString())
                {
                    if (listParts.Count(e => e.PartsDemand == "索赔发货") > 0)//如果有需要索赔发货的才通知配件部
                    {
                        AgentEmailController.SendPartsEmail(obj.QltyId);
                        returnVal = 2;//发邮件
                    }
                    if (listParts.Count(e => e.PartsDemand == "供应商发货") > 0)//如果有需要供应商发货的才通知服务内群
                    {
                        AgentEmailController.SendPartsToIHISCESvrSupperEmail(obj.QltyId);
                        returnVal = 2;//发邮件
                    }
                    AgentEmailController.EndEmail(obj.QltyId);
                    returnVal = returnVal > 0 ? returnVal : 3;
                    AddUClaimDemageControl(obj);
                    
                }

                if (obj.ClaimState == ClaimState.品情驳回.ToString())
                {
                    AgentEmailController.NoPassEmail(obj.QltyId);
                    returnVal = 5;
                }

            }

            return returnVal;*/

        }
        /// <summary>
        /// 只是修改品情
        /// </summary>
        /// <param name="SvrUserId"></param>
        /// <param name="obj"></param>
        /// <param name="listImg"></param>
        /// <param name="listParts"></param>
        /// <returns>
        /// 小于0表示执行错误
        /// 大于0表示成功
        /// </returns>
        public static int Edit(int SvrUserId, AgentQltyInfo obj, List<AgentQltyImgInfo> listImg, List<AgentQltyPartsInfo> listParts)
        {
            obj.PartsRequestType = GetPartsRequestType(listParts);
            return AgentQltyDAL.SvrActEdit(null, obj, listImg, listParts, null) ? 1 : 0;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="QltyId"></param>
        /// <param name="CCPartEmail"></param>
        /// <param name="SvrScheme"></param>
        /// <returns>
        /// 0表示执行错误
        /// -1表示已执行过通知,不能再执行
        /// 1表示成功
        /// </returns>
        public static int UpdateApproveScheme(int QltyId, string[] CCPartEmail, string SvrScheme)
        {
            //TODO:需要邮件通知抄送人员
            string emails = "";
            StringBuilder sb = new StringBuilder();
            foreach (string item in CCPartEmail)
            {
                string _e = AgentConfigController.GetValue(item);
                if (string.IsNullOrEmpty(_e))
                    continue;
                emails += _e + ";";
                sb.Append(item);
                sb.Append(",");
            }
            //sb.ToString().TrimEnd(',');
            SEMailListInfo objemail = AgentEmailController.GetApproveSchemeEmail(QltyId, emails, SvrScheme, "3");

            return AgentQltyDAL.UpdateApproveScheme(QltyId, sb.ToString().TrimEnd(','), SvrScheme, objemail);

            // return 0;
        }
        public static DataTableCollection GetPagination(string WhereSql, int PageIndex, int PageSize, string Sort, ref int TotalCount)
        {
            return AgentQltyDAL.GetPagination(WhereSql, PageIndex, PageSize, Sort, ref TotalCount);
        }

        public static bool Delete(int QltyId)
        {
            return AgentQltyDAL.Delete(QltyId);
        }
        public static AgentQltyInfo GetByQltyId(int QltyId)
        {
            return CBO.FillObject<AgentQltyInfo>(AgentQltyDAL.GetByQltyId(QltyId));
        }
        /// <summary>
        /// 根据品情号获取品情信息
        /// </summary>
        /// <param name="ClaimsNumber"></param>
        /// <returns></returns>
        public static AgentQltyInfo GetByClaimsNumber(string ClaimsNumber)
        {
            AgentQltyInfo obj = CBO.FillObject<AgentQltyInfo>(AgentQltyDAL.GetByClaimsNumber(ClaimsNumber));
            if (obj == null)
                obj = new AgentQltyInfo();
            return obj;
        }

        /// <summary>
        /// 根据用户，获取最后一次配件收件信息
        /// </summary>
        /// <param name="UserId"></param>
        /// <returns></returns>
        public static DataTable GetLastPartsAcceptInfo(int UserId)
        {
            return AgentQltyDAL.GetLastPartsAcceptInfo(UserId);
        }
        /// <summary>
        /// 获取上传片,品情详细页上的
        /// </summary>
        /// <param name="QltyId"></param>
        /// <returns></returns>
        public static List<AgentQltyImgInfo> GetAllImgByQltyId_0(int QltyId)
        {
            List<AgentQltyImgInfo> list = GetAllImgByQltyIdAll(QltyId);
            return list.FindAll(e => e.FromType == 0);
        }
        /// <summary>
        ///  获取上传片,表示来源于品情处理进度
        /// </summary>
        /// <param name="QltyId"></param>
        /// <param name="FromType"></param>
        /// <returns></returns>
        public static List<AgentQltyImgInfo> GetAllImgByQltyId_1(int QltyId)
        {
            List<AgentQltyImgInfo> list = GetAllImgByQltyIdAll(QltyId);
            return list.FindAll(e => e.FromType == 1);
        }
        /// <summary>
        /// 2016-04-06 加入 IHISCE服务部品情回复 图片
        /// </summary>
        /// <param name="QltyId"></param>
        /// <returns></returns>
        public static List<AgentQltyImgInfo> GetAllImgByQltyId_2(int QltyId)
        {
            List<AgentQltyImgInfo> list = GetAllImgByQltyIdAll(QltyId);
            return list.FindAll(e => e.FromType == 2);
        }

        public static List<AgentQltyImgInfo> GetAllImgByQltyId_3(int QltyId)
        {
            List<AgentQltyImgInfo> list = GetAllImgByQltyIdAll(QltyId);
            return list.FindAll(e => e.FromType == 3);
        }
        private static List<AgentQltyImgInfo> GetAllImgByQltyIdAll(int QltyId)
        {
            return CBO.FillCollection<AgentQltyImgInfo>(AgentQltyDAL.GetAllImgByQltyId(QltyId));
        }

        /// <summary>
        /// 获取配件
        /// </summary>
        /// <param name="QltyId"></param>
        /// <returns></returns>
        public static List<AgentQltyPartsInfo> GetAllPartsByQltyId(int QltyId)
        {
            return CBO.FillCollection<AgentQltyPartsInfo>(AgentQltyDAL.GetAllPartsByQltyId(QltyId));
        }
        /// <summary>
        /// 根据品情号码查找所有配件
        /// </summary>
        /// <param name="ClaimsNumber"></param>
        /// <returns></returns>
        public static DataTable GetAllPartsByClaimsNumber(string ClaimsNumber)
        {
            return AgentQltyDAL.GetAllPartsByClaimsNumber(ClaimsNumber);
        }
        /// <summary>
        /// 获取配件 包括物流信息
        /// </summary>
        /// <param name="QltyId"></param>
        /// <returns></returns>
        public static DataTable GetAllPartsAndLogisticsByQltyId(int QltyId)
        {
            return AgentQltyDAL.GetAllPartsAndLogisticsByQltyId(QltyId);
        }

        /// <summary>
        /// 从信息平台中的 索赔来往明细帐中获取配件ERP发货日期 
        /// </summary>
        /// <param name="QltyId"></param>
        /// <returns></returns>
        public static string GetERPSendDate(int QltyId)
        {
            object r = AgentQltyDAL.GetERPSendDate(QltyId);
            try
            {

                return r.ToString();
            }
            catch { return ""; }


        }
        /// <summary>
        /// 更新配件发货情况数据
        /// </summary>
        /// <param name="QltyId"></param>
        /// <param name="SupperSendDate"></param>
        /// <param name="ERPSendDate"></param>
        /// <param name="RealSendDate"></param>
        /// <returns></returns>
        public static bool UpdatePartsSendDate(int QltyId, DateTime SupperSendDate, DateTime ERPSendDate, DateTime RealSendDate)
        {
            return AgentQltyDAL.UpdatePartsSendDate(QltyId, SupperSendDate, ERPSendDate, RealSendDate);
        }
        /// <summary>
        /// 排除品情后3天内要回访
        /// </summary>
        /// <param name="QltyId"></param>
        /// <param name="_AgentAcceptDate"></param>
        /// <param name="_AgentRecoveryDate"></param>
        /// <returns></returns>
        public static bool UpdatePartsAcceptDate(int QltyId, DateTime _AgentAcceptDate, DateTime _AgentRecoveryDate, DateTime _PartsInDate, string ImgHY, List<AgentQltyImgInfo> imgList)
        {
            bool rsv = AgentQltyDAL.UpdatePartsAcceptDate(QltyId, _AgentAcceptDate, _AgentRecoveryDate, _PartsInDate, ImgHY, imgList);
            if (rsv)
            {
                AgentQltyInfo obj = GetByQltyId(QltyId);
                if (obj.IsVisit && _AgentRecoveryDate != DateTime.MinValue)
                {
                    DateTime VisitDate = DateTime.Now.AddDays(3);//默认为排除品情后3天内要回访
                    AgentQltyVisitController.Add(QltyId, obj.AgentName, DateTime.Now, VisitDate);
                }
            }
            return rsv;

        }

        /// <summary>
        /// 平均维修时间
        /// </summary>
        /// <param name="WhereSql"></param>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <param name="Sort"></param>
        /// <param name="TotalCount"></param>
        /// <returns></returns>
        public static DataTableCollection MTTRGetPagination(string WhereSql, int PageIndex, int PageSize, string Sort, ref int TotalCount)
        {
            return AgentQltyDAL.MTTRGetPagination(WhereSql, PageIndex, PageSize, Sort, ref TotalCount);
        }

        /// <summary>
        /// 发货时间、到货时间及故障排除时间如果3天内未填入，是否可以每天发一份邮件跟催询问
        /// </summary>
        /// <param name="type">1.发货时间  2.到货时间 3.故障排除时间</param>
        /// <returns></returns>
        public static List<AgentQltyInfo> GetWarnInputDate(int type)
        {
            return CBO.FillCollection<AgentQltyInfo>(AgentQltyDAL.GetWarnInputDate(type));
        }
        /// <summary>
        /// 获取驳回列表
        /// </summary>
        /// <param name="QltyId"></param>
        /// <returns></returns>
        public static DataTable AgentQltyReject_GetAllByQltyId(int QltyId)
        {
            return AgentQltyDAL.AgentQltyReject_GetAllByQltyId(QltyId);
        }

        public static DataTableCollection AgentQltyPartsGetPagination(string WhereSql, int PageIndex, int PageSize, string Sort, ref int TotalCount)
        {
            return AgentQltyDAL.AgentQltyPartsGetPagination(WhereSql, PageIndex, PageSize, Sort, ref TotalCount);
        }

        public static bool UpdateInStock(int QltyId, int ID, DateTime InDate, decimal InQuantity, string InOrderNum, string InRemark, decimal InWuPan, bool IsPan, int InGuiFan, string GoTrack, string GOTrackDate)
        {
            SEMailListInfo objEmail = null;
            if (InQuantity > 0 && InDate > DateTime.MinValue)
                objEmail = AgentEmailController.GetOldPartReturn(QltyId);

            return AgentQltyDAL.UpdateInStock(ID, InDate, InQuantity, InOrderNum, InRemark, InWuPan, IsPan, InGuiFan, GoTrack, GOTrackDate, objEmail);
        }

        /// <summary>
        /// 旧件返回规范
        /// </summary>
        /// <param name="f"></param>
        /// <param name="t"></param>
        /// <returns></returns>
        public static DataTable GetInGuiFan(string f, string t)
        {
            return AgentQltyDAL.GetInGuiFan(f, t);
        }
        public static DataTable GetWuPaiBiLi(string f, string t)
        {
            return AgentQltyDAL.GetWuPaiBiLi(f, t);
        }
        /// <summary>
        /// 索赔件自给率
        /// </summary>
        /// <param name="f"></param>
        /// <param name="t"></param>
        /// <returns></returns>
        public static DataTable GetSPZGL(string f, string t)
        {
            return AgentQltyDAL.GetSPZGL(f, t);
        }
        /// <summary>
        ///  2016-10-21 质管部 指派管制及对策管制
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static bool UpdateZLAssign(AgentQltyInfo obj)
        {
            return AgentQltyDAL.UpdateZLAssign(obj);
        }

        public static DataTable GetIsYJGLReport(string sqlwhere)
        {
            return AgentQltyDAL.GetIsYJGLReport(sqlwhere);
        }
        public static DataTable GetNoNormativeReport(string sqlwhere)
        {
            return AgentQltyDAL.GetNoNormativeReport(sqlwhere);
        }
    }
}
